﻿using System;
using System.ComponentModel.Design;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using Jwell.Template.App.MyWindow;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Jwell.Template.App
{
    /// <summary>
    /// Command handler
    /// </summary>
    internal sealed class AddSolutionCommand
    {
        /// <summary>
        /// Command ID.
        /// </summary>
        public const int CommandId = 0x0100;

        /// <summary>
        /// Command menu group (command set GUID).
        /// </summary>
        public static readonly Guid CommandSet = new Guid("0fd52e60-7522-4578-ac89-5033255fdcbe");

        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly Package package;

        /// <summary>
        /// Initializes a new instance of the <see cref="AddSolutionCommand"/> class.
        /// Adds our command handlers for menu (commands must exist in the command table file)
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        private AddSolutionCommand(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;

            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                commandService.AddCommand(menuItem);
            }
        }

        /// <summary>
        /// Gets the instance of the command.
        /// </summary>
        public static AddSolutionCommand Instance
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the service provider from the owner package.
        /// </summary>
        private IServiceProvider ServiceProvider
        {
            get
            {
                return this.package;
            }
        }

        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static void Initialize(Package package)
        {
            Instance = new AddSolutionCommand(package);
        }

        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            var s = Assembly.GetExecutingAssembly().GetManifestResourceInfo("Resources/test.txt");
            string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            string title = "AddSolutionCommand";
            var option = new SolutionOption();
            var dialog = new SolutionOptionsDialog(option);
            var result = dialog.ShowDialog();
            var solution = this.ServiceProvider.GetService(typeof(IVsPersistSolutionProps)) as IVsPersistSolutionProps;
            if (result == DialogResult.OK)
            {
                //表示创建新的解决方案
                var creator=new SolutionCreator(this.ServiceProvider,option);
                creator.CreateSolutionOnly(option.SolutionPath,option.SolutionName);
                creator.CreateProjecsFromTemplate(@"C:\Code\WorkCode\Jwell\Services\Jwell.SSO");
            }
        }
    }
}
